#include "World.h"

World::World(): textureManager(0), shapeManager(0), camera(0), map(0)
{
}

World::~World()
{
}

Map* World::GetMap()
{
	if (0 == map)
	{
		map = NewMap();
	}

	return map;
}

TextureManager* World::GetTextureManager()
{
	if (0 == textureManager)
	{
		textureManager = NewTextureManager();;
	}

	return textureManager;
}

ShapeManager* World::GetShapeManager()
{
	if (0 == shapeManager)
	{
		shapeManager = NewShapeManager();
	}

	return shapeManager;
}

Camera* World::GetCamera()
{
	if (0 == camera)
	{
		camera = NewCamera();
	}

	return camera;
}

Camera* World::NewCamera()
{
	return new Camera();
}

TextureManager* World::NewTextureManager()
{
	return new TextureManager();
}

ShapeManager* World::NewShapeManager()
{
	return new ShapeManager();
}

Map* World::NewMap()
{
	return new Map();
}

void World::RemoveShape(unsigned int cellx, unsigned int celly)
{
	// Get a pointer to the referenced cell
	Cell *cell = GetMap()->GetCell(cellx, celly);
	// And a pointer to its shape
	const Shape *oldShape = cell->GetShape();

	// Does this cell already have a shape?
	if (oldShape)
	{
		// Then remove it from the shape manager
		GetShapeManager()->RemoveShape(oldShape);
		// And remove it from the cell
		cell->RemoveShape();
	}
}

void World::SetShape(unsigned int cellx, unsigned int celly, const Shape& shape)
{
	// Get a pointer to the referenced cell
	Cell *cell = GetMap()->GetCell(cellx, celly);

	// Remove the cell's current shape (if any)
	RemoveShape(cellx, celly);
	
	// Add a new shape to the shape manager
	Shape *newShape = GetShapeManager()->AddShape(shape);
	// And set the cell's new shape
	cell->SetShape(newShape);
}

